from django.shortcuts import render
from SSQ.getweb import get_history_phasedata_from_360cp
from django.http import HttpResponse, JsonResponse
import simplejson
from SSQ import general_and_constant


# 返回各期数据
def history_data(request):
    if request.method == "GET":
        if request.is_ajax():
            if request.GET.get("cmd_class") == "query":
                command = request.GET.get("command")
                if command == 'get_history_data':  # 获取历史开奖数据，默认返回近100期
                    data = general_and_constant.get_history_data()
                    return JsonResponse(data, safe=False)
                if command == 'get_history_by_phasenum':#指定期号查询
                    phasenum = request.GET.get("phasenum")
                    data = general_and_constant.get_phasenum_history_data(phasenum)
                    return JsonResponse(data, safe=False)
                if command == 'get_history_by_multi_phasenum':#指定期数查询
                    phasenum = request.GET.get("phasenum")
                    phasenum = int(phasenum)
                    data = general_and_constant.get_history_data(phasenum)
                    return JsonResponse(data, safe=False)
                if command == 'year':#获取年份信息

                    data = general_and_constant.query_history_yearlist()
                    return JsonResponse(data, safe=False)
                if command == 'get_history_by_year':#根据你年份获取开奖信息
                    year = request.GET.get("year")
                    data = general_and_constant.get_history_by_year(year)
                    return JsonResponse(data, safe=False)
                if command == 'refresh_history_data':#刷新开奖数据

                    data = general_and_constant.refresh_history_data()
                    return JsonResponse(data, safe=False)
    # bonus_info = general_and_constant.query_recent_bonue_info()
    # bonus_info = 'test' #TODO：是否还需要传参，需要考虑
    # get_history_phasedata_from_360cp('phasenum')  # 更新开奖数据 TODO：需要优化，太耗时间
    return render(request, 'SSQ/history_data.html', {'bonus_info': 'bonus_info'})


# 返回bet_data页面
def bet_data(request):

    if request.method == "GET":
        if request.is_ajax():
            if request.GET.get("cmd_class") == "query":
                command = request.GET.get("command")
                if command == 'get_bet_data':  # 获取投注信息
                    data = general_and_constant.get_bet_data()
                    return JsonResponse(data, safe=False)
                if command == 'get_bet_by_phasenum':#指定期号查询
                    phasenum = request.GET.get("phasenum")
                    data = general_and_constant.get_phasenum_bet_data(phasenum)
                    return JsonResponse(data, safe=False)
                if command == 'get_bet_by_multi_phasenum':#指定期数查询
                    phasenum = request.GET.get("phasenum")
                    phasenum = int(phasenum)
                    data = general_and_constant.get_bet_data(phasenum)
                    return JsonResponse(data, safe=False)
                if command == 'get_win_bet_data':#查询所有中奖投注
                    data = general_and_constant.query_win_bet_data()
                    return JsonResponse(data, safe=False)
                if command == 'get_wait_bet_data':#查询所有待开奖投注
                    data = general_and_constant.query_wait_bet_data()
                    return JsonResponse(data, safe=False)
                if command == 'btn_query_not_win_data':#查询所有待开奖投注
                    data = general_and_constant.btn_query_not_win_data()
                    return JsonResponse(data, safe=False)
                if command == 'year':#获取年份信息

                    data = general_and_constant.query_bet_yearlist()
                    return JsonResponse(data, safe=False)
                if command == 'get_bet_by_year':#根据你年份获取开奖信息
                    year = request.GET.get("year")
                    data = general_and_constant.get_bet_by_year(year)
                    return JsonResponse(data, safe=False)

    if request.method == "POST":
        if request.is_ajax():
            if request.POST.get("cmd_class") == "del":
                command = request.POST.get("command")
                if command == 'all':  # 删除所有投注信息
                    data = general_and_constant.del_bet_data('all')
                    return JsonResponse(data, safe=False)
                if command == 'selectlist':  # 删除所有投注信息
                    selectlist = request.POST.get("selectlist")# 返回一个字符串
                    selectlist = simplejson.loads(selectlist) # 解析字符串，返回一个list
                    data = general_and_constant.del_bet_data(selectlist)
                    return JsonResponse(data, safe=False)
    general_and_constant.check_all_bonus()  # 检测是否还有未匹配的数据，放在此处可提高显示效率
    return render(request, 'SSQ/bet_data.html')


# 返回bet页面
def bet(request):
    if request.method == "GET":
        return render(request, 'SSQ/bet.html')
    if request.method == "POST":
        if request.is_ajax():
            if request.POST.get("cmd_class") == "write":
                command = request.POST.get("command")
                if command == "common":
                    red1 = request.POST.getlist("reda[]")  # reda后面一定要夹加[]，否则无法获取数组
                    blue1 = request.POST.getlist("bluea[]")
                    multiple = request.POST.get("multiple")  # 倍数
                    # add_bet = request.POST.get("add_bet")  # 是否追加
                    # if add_bet == 'true': add_bet = True
                    # else: add_bet = False
                    add_bet = False#双色球没有追加

                    red1.sort()
                    blue1.sort()
                    phasenum = request.POST.get("phasenum")  # 期号
                    notelist = general_and_constant.combinations_notes(red1, blue1)

                    general_and_constant.save_notedata_to_DB(phasenum, notelist, multiple, add_bet)
                    return HttpResponse('ok')
                if command == "dantuo":
                    reddan = request.POST.getlist("reddan[]")  # reda后面一定要夹加[]，否则无法获取数组
                    bluedan = request.POST.getlist("bluedan[]")
                    redtuo = request.POST.getlist("redtuo[]")

                    multiple = request.POST.get("multiple")  # 倍数
                    # add_bet = request.POST.get("add_bet")  # 是否追加
                    # if add_bet == 'true': add_bet = True
                    # else: add_bet = False
                    add_bet = False  # 双色球没有追加
                    reddan.sort()
                    redtuo.sort()
                    bluedan.sort()

                    phasenum = request.POST.get("phasenum")
                    notelist = general_and_constant.dantuo_combinations_notes(reddan, redtuo, bluedan)

                    general_and_constant.save_notedata_to_DB(phasenum, notelist, multiple, add_bet)
                    return HttpResponse('ok')
    return render(request, 'SSQ/bet.html')